using System;
using System.Collections.Generic;
using GeneticAlgorithms.Genomes;

namespace GeneticAlgorithms.Tools
{
    /// <summary>
    /// Compares genomes based on their fitness.
    /// </summary>
    public class FitnessComparer : IComparer<IGenomeBase>
    {
        #region IComparer<IGenomeBase> Members

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        /// Value Condition Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        public int Compare(IGenomeBase x, IGenomeBase y)
        {
            return Math.Sign(x.Fitness.Value - y.Fitness.Value);
        }

        #endregion

        private static FitnessComparer instance = new FitnessComparer();

        /// <summary>
        /// Gets the default instance for the fitness valueExtractor.
        /// </summary>
        /// <value>The default instance.</value>
        public static FitnessComparer Default { get { return instance; } }
    }
}
